﻿using System;
using System.Web.UI;
using BUS;

public partial class UserControl_Login : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
        {
            MultiView1.SetActiveView(ViewLogIn);
        }
        else
        {
            MultiView1.SetActiveView(ViewLogOut);
            var kh = new KhachHangBUS() { TenDangNhap = (string)Session["user"] };
            if (kh.GetbyUserName())
            {
                hplUser.Text = string.Format("<b> {0}</b>", kh.HoTen);
                hplUser.NavigateUrl = string.Format("~/AccountDetails.aspx?user={0}", kh.TenDangNhap.EncryptMD5());
            }
        }
    }

    protected void btnSumit_Click(object sender, EventArgs e)
    {
        var kh = new KhachHangBUS { TenDangNhap = txtTenDangNhap.Text, MatKhau = txtMatKhau.Text };
        if (App_Code.Account.CheckLogin(txtTenDangNhap.Text, txtMatKhau.Text))
        {
            MultiView1.SetActiveView(ViewLogOut);
            hplUser.Text = string.Format("<b>{0}</b>", Session["hoten"]);
            hplUser.NavigateUrl = string.Format("~/AccountDetails.aspx?user={0}", Session["user"].ToString().EncryptMD5());
        }
        else
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alert", "alert('Đăng nhập chưa có đúng đâu');", true);

    }
    protected void btnLogOut_Click(object sender, EventArgs e)
    {
        if (Session["user"] == null) return;
        Session["user"] = null;
        MultiView1.SetActiveView(ViewLogIn);
    }
}